package com.cy.config;

import com.cy.pojo.Configuration;
import com.cy.pojo.MappedStatement;
import com.cy.pojo.SqlCommandType;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Locale;

public class XmlMappedStatement {

    public void parse(Configuration configuration, InputStream mapperResourceIn) throws DocumentException, ClassNotFoundException {
        Document mapperDocument = new SAXReader().read(mapperResourceIn);
        Element mapperRootElement = mapperDocument.getRootElement();

        String namespace = mapperRootElement.attributeValue("namespace");

        List<Element> list = mapperRootElement.selectNodes("//select");
        for (Element mapperElement : list) {

            String id = mapperElement.attributeValue("id");
            String resultType = mapperElement.attributeValue("resultType");
            String paramType = mapperElement.attributeValue("paramType");

            Class<?> resultTypeClass = getClassByType(resultType);
            Class<?> paramTypeClass = getClassByType(paramType);
            String sql = mapperElement.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultTypeClass);
            mappedStatement.setParamType(paramTypeClass);
            mappedStatement.setSql(sql);
            mappedStatement.setSqlCommandType(SqlCommandType.SELECT);

            //key = namespace.id
            String key = namespace + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }

        //添加所有insert的标签
        List<Element> insertList = mapperRootElement.selectNodes("//insert");
        List<Element> updateList = mapperRootElement.selectNodes("//update");
        List<Element> deleteList = mapperRootElement.selectNodes("//delete");

        //两种标签处理方法一样
        insertList.addAll(updateList);
        insertList.addAll(deleteList);
        for (Element mapperElement : insertList) {

            String elementName = mapperElement.getName();

            String id = mapperElement.attributeValue("id");
            String paramType = mapperElement.attributeValue("paramType");

            Class<?> paramTypeClass = getClassByType(paramType);
            String sql = mapperElement.getTextTrim();

            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamType(paramTypeClass);
            mappedStatement.setSql(sql);

            //根据标签名称获取sql类型
            SqlCommandType sqlCommandType = SqlCommandType.valueOf(elementName.toUpperCase(Locale.ENGLISH));
            mappedStatement.setSqlCommandType(sqlCommandType);

            //key = namespace.id
            String key = namespace + "." + id;
            configuration.getMappedStatementMap().put(key, mappedStatement);
        }
    }

    /**
     * 根据类型获得类
     * @param type
     * @return
     */
    private Class<?> getClassByType(String type) throws ClassNotFoundException {
        if (type == null){
            return null;
        }
        Class<?> classes = Class.forName(type);
        return classes;
    }

}
